/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package corejava;

import java.awt.Point;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
import java.util.TreeSet;



/**
 *
 * @author HuynhNB
 */
public class CoreJava {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
        // Set example
        int count[] = {1,2,3,35,64,1,4,5,6,7};
        
        Set<Integer> set = new HashSet<Integer>();
        
        try{
            for(int i=0; i<10; i++){
            set.add(count[i]);
            }
            //set.add(null);<-- java.lang.NullPointerException
            // no order of SET
            System.out.println(set);
            TreeSet sortedSet=new TreeSet<Integer>(set);
            System.out.println("The sorted list is:");
            System.out.println(sortedSet);

            System.out.println("The First element of the set is: "+
            (Integer)sortedSet.first());
            System.out.println("The last element of the set is: "+
            (Integer)sortedSet.last());
                       
            
        }catch(Exception e){
            e.printStackTrace();
        }
         
        
       // List
        // order item
        // can be null
        int number[] = {42,42,65,87,78,98,54};
        List<Integer> ls = new ArrayList<Integer>();
        
        try{
            for(int i=0; i<7; i++){
                ls.add(number[i]);
            }
            ls.add(null);// list can add null
             System.out.println("the List is: ");
             System.out.println(ls);                       
            
        }catch(Exception e){}
       // Queue
        LinkedList <Integer>queue_f = new LinkedList<Integer>();
        LinkedList <Integer>queue_l = new LinkedList<Integer>();
            for(int i=0; i<7; i++){
            queue_f.addFirst(number[i]);
            queue_l.addLast(number[i]);
            }
            System.out.println("The Oueue with add first is: ");
            System.out.println(queue_f);
            queue_f.removeLast();
            System.out.println("After removing last element the queue is: "+ queue_f);
            
            
            System.out.println("The Oueue with add last is: ");
            System.out.println(queue_l);
            queue_l.removeLast();
            System.out.println("After removing last element the queue is: "+ queue_l);
            
            // Map example
            String days[]={"Sunday", "Monday", "Tuesday", "Wednesnday",
                         "Thursday", "Friday", "Saturday"};
            Map<Integer, String> map = new HashMap();
            try{
                for(int i=0; i<7; i++){
                  map.put(i, days[i]);
                }
                 System.out.println(map);
                 TreeMap tMap=new TreeMap(map);
                 //Rerieving all keys
                System.out.println("Keys of tree map: " + tMap.keySet());
                //Rerieving all values
                System.out.println("Values of tree map: " + tMap.values());
                //Rerieving the First key and its value
                System.out.println("First key: " + tMap.firstKey() + 
                              " Value: " + tMap.get(tMap.firstKey()) + "\n");

                //Removing the first key and value
                System.out.println("Removing first data: " 
                                      + tMap.remove(tMap.firstKey()));
                System.out.println("Now the tree map Keys: " + tMap.keySet());
                System.out.println("Now the tree map contain: " + tMap.values() + "\n");
                //Rerieving the Last key and value
                System.out.println("Last key: " + tMap.lastKey() + 
                                   " Value: " + tMap.get(tMap.lastKey()) + "\n");  
                //Removing the last key and value
                System.out.println("Removing last data: " + tMap.remove(tMap.lastKey()));
                System.out.println("Now the tree map Keys: " + tMap.keySet());
                System.out.println("Now the tree map contain: " + tMap.values());
                 
            }
            catch(Exception e){}
            // compare muable and immuable obj
            //muable
            Point myPoint = new Point( 0, 0 );
            System.out.println( myPoint );
            myPoint.setLocation( 1.0, 0.0 );
            System.out.println( myPoint );
            StringBuffer _sf = new StringBuffer("old String");
            System.out.println( _sf );
            _sf.append("test");
            System.out.println( _sf );
            
            //immuable
            String myString = new String( "old String" );
            System.out.println( myString );
            myString.replaceAll( "old", "new" );
            myString.toLowerCase();
            System.out.println( myString );
            
            
            String myCache = myString;
            System.out.println( "equal: " + myString.equals( myCache ) );
            System.out.println( "same:  " + ( myString == myCache ) );

            myString = "not " + myString;
            System.out.println( "equal: " + myString.equals( myCache ) );
            System.out.println( "same:  " + ( myString == myCache ) );
            System.out.println( myString );
            
            

    } // end of main method
    
} // end of class
